package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    @Reference
    private RoleService roleService;
    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return permissionService.findPage(queryPageBean);
    }

    /**
     * 根据role_id查询所有执行权限，并且排序
     *
     * @param id
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findAllPmsByRole")
    public PageResult findAllPmsByRole(Integer id, @RequestBody QueryPageBean queryPageBean) {
        return permissionService.findAllPmsByRole(id, queryPageBean);
    }

    @RequestMapping("/findAllPms")
    public PageResult findAllPms(@RequestBody QueryPageBean queryPageBean){
        return permissionService.findAllPms(queryPageBean);
    }

    /**
     * 添加权限
     *
     * @return
     */
    @RequestMapping("/handleAdd")
    public Result handleAdd(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    /**
     * 编辑更新权限项
     *
     * @param permission
     * @return
     */
    @RequestMapping("/handleEdit")
    public Result handleEdit(@RequestBody Permission permission) {
        try {
            permissionService.edit(permission);
            return new Result(true, MessageConstant.UPDATE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_PERMISSION_FAIL);
        }
    }

    /**
     * 根据id删除权限项
     *
     * @param id
     * @return
     */
    @RequestMapping("/handleDelete")
    public Result handleDelete(Integer id) {
        try{
            permissionService.delete(id);
            return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
        }catch (Exception e){
            if(e.getMessage().equals("权限已关联角色")){
                return new Result(false,"权限已关联角色");
            }
            return new Result(false,MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    /**
     * 根据权限id进行数据的回显
     *
     * @param id
     * @return
     */
    @RequestMapping("/handleUpdate")
    public Result handleUpdate(Integer id) {
        try {
            Permission permission = permissionService.update(id);
            return new Result(true, MessageConstant.GET_PERMISSION_SUCCESS, permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_PERMISSION_FAIL);
        }
    }

}
